/**
 * Copyright (c) 2019-2022 fengt All rights reserved.
 *
 * 版权所有，侵权必究！
 */

package com.min.main.service;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.core.util.StrUtil;
import com.min.main.constant.RedisKey;
import com.min.main.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.OutputStream;

/**
 * 验证码
 * @author fengt
 */
@Service("captchaService")
public class CaptchaService {

    @Autowired
    RedisUtils redis;

    public void getCaptcha(String uuid, OutputStream os) {
        ShearCaptcha lineCaptcha = CaptchaUtil.createShearCaptcha(200, 50, 4,1);
        lineCaptcha.write(os);
        redis.set(RedisKey.CAPTCHA + uuid, lineCaptcha.getCode());
    }

    public boolean validate(String uuid, String code) {
        final String s = redis.get(RedisKey.CAPTCHA + uuid);
        return StrUtil.isNotBlank(s) && s.equals(code);
    }
}
